package com.xinqi.modules.sys.textbook.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinqi.common.base.enums.YesNoEnum;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.sys.textbook.domain.CloudTextbookInfoEntity;
import com.xinqi.modules.sys.textbook.dto.req.CloudTextbookInfoQueryDTO;
import com.xinqi.modules.sys.textbook.dto.rsp.CloudTextbookInfoResultDTO;
import com.xinqi.modules.sys.textbook.manager.CloudTextbookInfoManager;
import com.xinqi.modules.sys.textbook.mapper.CloudTextbookInfoMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 云课本基本信息管理层实现
 *
 * @author: linkai
 * @date: 2023/04/25
 */
@Service
@RequiredArgsConstructor
public class CloudTextbookInfoManagerImpl extends BaseServiceImpl<CloudTextbookInfoMapper, CloudTextbookInfoEntity> implements CloudTextbookInfoManager {

    /**
     * 自定义分页查询
     */
    @Override
    public Page<CloudTextbookInfoResultDTO> findPage(Page<Object> page, CloudTextbookInfoQueryDTO query) {
        return this.baseMapper.findPage(page, query);
    }

    /**
     * 自定义列表查询
     */
    @Override
    public List<CloudTextbookInfoResultDTO> findList(CloudTextbookInfoQueryDTO query) {
        return this.baseMapper.findList(query);
    }

    /**
     * 根据id查询 云课本基本信息数据
     */
    @Override
    public CloudTextbookInfoEntity findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return getById(id);
    }

    /**
     * 根据id删除 云课本基本信息数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return this.removeById(id);
    }

    @Override
    public Boolean isExistName(String name, Long oId) {
        LambdaQueryWrapper<CloudTextbookInfoEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CloudTextbookInfoEntity::getName, name);
        wrapper.eq(CloudTextbookInfoEntity::getIsDelete, YesNoEnum.NO.ordinal());
        wrapper.ne(oId != null, CloudTextbookInfoEntity::getId, oId);
        return count(wrapper) > 0;

    }

    @Override
    public CloudTextbookInfoEntity findByName(String name) {
        LambdaQueryWrapper<CloudTextbookInfoEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CloudTextbookInfoEntity::getName, name);
        wrapper.eq(CloudTextbookInfoEntity::getIsDelete, YesNoEnum.NO.ordinal());
        return getOne(wrapper);
    }

    /**
     * 通过版本ID查询云课本已绑定的学段ID集合
     */
    @Override
    public List<Long> findByVersionIdList(Long versionId) {
        return this.baseMapper.findByVersionIdList(versionId);
    }

    /**
     * 通过学段ID查询云课本已绑定的年纪ID集合
     */
    @Override
    public List<Long> findByStageIdList(Long stageId) {
        return this.baseMapper.findByStageIdList(stageId);
    }

    /**
     * 通过年纪ID查询云课本已绑定的学科ID集合
     */
    @Override
    public List<Long> findByGradeIdList(Long gradeId) {
        return this.baseMapper.findByGradeIdList(gradeId);
    }

}
